package com.lmh.system.controller.sysuser;

import com.lmh.common.core.constants.HttpConstants;
import com.lmh.common.core.controller.BaseController;
import com.lmh.common.core.domain.R;
import com.lmh.common.core.domain.vo.LoginUserVO;
import com.lmh.system.domain.sysuser.dto.SysUserDTO;
import com.lmh.system.domain.sysuser.dto.SysUserSaveDTO;
import com.lmh.system.service.sysuser.impl.SysUserServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/sysUser")
@RestController
@Tag(name = "管理员用户API")
public class SysUserController extends BaseController{
    @Autowired
    private SysUserServiceImpl sysUserServiceImpl;

    @PostMapping("/add")
    @Operation(summary = "新增管理员",description = "根据提供的信息新增管理员用户")
    @ApiResponse(responseCode = "1000",description = "操作成功")
    @ApiResponse(responseCode = "2000",description = "服务繁忙请稍后重试")
    @ApiResponse(responseCode = "3101",description = "用户已存在")
    public R<Void> add(@RequestBody SysUserSaveDTO sysUserSaveDTO) {
        return toR(sysUserServiceImpl.add(sysUserSaveDTO));
    }

    @PostMapping("/login")
    @Operation(summary = "管理员登录",description = "根据提供的信息进行管理员登录")
    @Parameters({
            @Parameter(name = "sysUserDTO",in = ParameterIn.QUERY,description = "管理员用户和密码")
    })
    @ApiResponse(responseCode = "1000",description = "操作成功")
    @ApiResponse(responseCode = "2000",description = "服务繁忙请稍后重试")
    @ApiResponse(responseCode = "3101",description = "用户已存在")
    public R<String> login(@RequestBody SysUserDTO sysUserDTO) {
        return sysUserServiceImpl.login(sysUserDTO.getUserAccount(), sysUserDTO.getPassword());
    }

    @RequestMapping("/info")
    @Operation(summary = "获取用户信息",description = "用户登录之后获取用户的昵称")
    public R<LoginUserVO> info(@RequestHeader(HttpConstants.AUTHENTICATION) String token) {
        return sysUserServiceImpl.info(token);
    }

    @Operation(summary = "用户登出",description = "用户登出，回到登录页面")
    @DeleteMapping("/logout")
    public R<Void> logout(@RequestHeader(HttpConstants.AUTHENTICATION) String token) {
        return toR(sysUserServiceImpl.logout(token));
    }
}
